package com.wecloud.core.utils;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.util.TokenBuffer;

public class JsonUtils {
	static ObjectMapper mapper = new ObjectMapper();
	/**
	 * JSON序列化
	 * @param data
	 * @return
	 */
	public static String serialize(Object data){
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			TokenBuffer buffer = new TokenBuffer(mapper);
			mapper.writeValue(buffer, data);
			return mapper.readTree(buffer.asParser()).toString();
		} catch (Throwable e) {
			return null;
		}
	}
	/**
	 * json反序列化
	 * @param jsonData
	 * @param clazz
	 * @return
	 */
	public static <T> T deserialize(String jsonData, Class<T> clazz) {
		try {
			return mapper.readValue(jsonData, clazz);
		} catch (Exception e) {
			return null;
		}
	}
}
